package com.spzx.payment.service.impl;


import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.order.api.RemoteOrderInfoService;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.payment.config.AlipayConfig;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.IAlipayService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IAlipayServiceImp  implements IAlipayService {

    @Autowired
    private RemoteOrderInfoService remoteOrderInfoService;

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {

        //判断支付日志是否已经记录

        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(queryWrapper);

        if(paymentInfo != null){
            return null;
        }


        //获取订单信息
        R<OrderInfo> orderInfoResult = remoteOrderInfoService.getByOrderNo(orderNo, SecurityConstants.INNER);
        if(R.FAIL == orderInfoResult.getCode()){
            throw new ServiceException(orderInfoResult.getMsg());
        }
        OrderInfo orderInfo = orderInfoResult.getData();

        //生成日志
        paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPayType((byte) 2);//支付宝
        paymentInfo.setPaymentStatus(0);//未支付
        paymentInfo.setAmount(orderInfo.getTotalAmount()); //总金额
        paymentInfo.setUpdateBy(orderInfo.getUpdateBy());
        paymentInfo.setCreateBy(orderInfo.getCreateBy());
        paymentInfo.setRemark(orderInfo.getRemark());
        paymentInfo.setContent(orderInfo.getOrderItemList().get(0).getSkuName() + "等" + orderInfo.getOrderItemList().size() + "件商品");
        paymentInfoMapper.insert(paymentInfo);




        //--------组装支付请求参数-------------
        //组装公共参数的可选参数
        // 构造请求参数以调用接口
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(AlipayConfig.return_payment_url);
        request.setNotifyUrl(AlipayConfig.notify_payment_url);

        //业务参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        //设置商户订单号
        model.setOutTradeNo(orderNo);
        //设置订单总金额
        model.setTotalAmount(orderInfo.getTotalAmount().toString());
        // 设置订单标题
        model.setSubject(orderInfo.getOrderItemList().get(0).getSkuName() + "等" + orderInfo.getOrderItemList().size() + "件商品");
        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");  //固定的
        //将业务参数设置在请求参数中
        request.setBizModel(model);

        //把参数全部组装完毕，需要包装成form表单请求给支付宝检验
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");


        String form  = response.getBody();

        return form;
    }
}
